import { instance } from '@/utils/request'

export default {
  namespaced: true,
  state () {
    return {
      list: [],
      defaultAddress: {}
    }
  },
  getters: {},
  mutations: {
    loadAddressList (state, obj) {
      // console.log(`addressList = ${JSON.stringify(obj.list)}, defaultAddressId = ${JSON.stringify(obj.defaultAddress)}`)
      state.list = obj.list
      state.defaultAddress = obj.defaultAddress
    }
  },
  actions: {
    async getAddressList (context) {
      const { list } = await instance.get('/address/list')
      const { defaultId } = await instance.get('/address/defaultId')
      // console.log(defaultId)
      context.commit('loadAddressList', {
        list: list,
        defaultAddress: list.find(e => e.address_id === defaultId)
      })
    }
  }
}
